package com.cjc.myimageload.tools;

import java.io.IOException;
import java.io.InputStream;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

/**
 * 
 ***************************************************** 
 * <hr>
 * <dt><span class="strong">类功能简介:</span></dt>
 * <dd>OkHttp 数据请求GIT、POST方法</dd>
 * <dt><span class="strong">创建时间:</span></dt>
 * <dd>2015-10-15 下午2:44:41</dd>
 * <dt><span class="strong">公司:</span></dt>
 * <dd>SHLD</dd>
 * 
 * @author aa1000777 - Email:aa1000777@qq.com
 ***************************************************** 
 */
public class RequestData {
	public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
	public static OkHttpClient client = new OkHttpClient();

	/**
	 ***************************************************** 
	 * 方法简介: GET 完整URL（http://192.168.1.123:88/UserService.svc/users）
	 ***************************************************** 
	 */
	public static String getUrlJson(String url) throws IOException {
		OkHttpClient client = new OkHttpClient();
		Request request = new Request.Builder()
		// .header("X-Yizs-ID", "868331011992179")//添加请求头文件
		// .addHeader("X-Yizs-Key", "123456789")//添加请求头文件
				.url(url).build();
		Response response = client.newCall(request).execute();
		return response.body().string();
	}

	/**
	 ***************************************************** 
	 * 方法简介: GET 只有方法名
	 ***************************************************** 
	 */
	public static String getJson(String method) throws IOException {
		return getUrlJson(Constants.URL + method);
	}

	/**
	 ***************************************************** 
	 * 方法简介: GET 有方法名、有参数
	 ***************************************************** 
	 */
	public static String getJson(String method, String parameter) throws IOException {
		return getJson(method + parameter);
	}
	/**
	 * 
	 ***************************************************** 
	 * 方法简介:git
	 * 
	 * @param url
	 *            http://192.168.1.123:88/UserService.svc/users
	 * @return
	 * @throws IOException
	 ***************************************************** 
	 */
	public static InputStream getInputStream(String url) throws IOException {
		 System.out.println("git请求的URL:" + url);
		OkHttpClient client = new OkHttpClient();
		Request request = new Request.Builder().url(url).build();
		Response response = client.newCall(request).execute();
		return response.body().byteStream();
	}
	/**
	 ***************************************************** 
	 * 方法简介: POST
	 * 
	 * @param method
	 *            方法名
	 * @param parameter
	 *            Json格式参数
	 ***************************************************** 
	 */
	public static String postJson(String method, String parameter) throws IOException {
		String url = Constants.URL + method;
		RequestBody body = RequestBody.create(JSON, parameter);
		Request request = new Request.Builder()
		// .header("X-Yizs-ID", "868331011992179")//添加请求头文件
		// .addHeader("X-Yizs-Key", "123456789")//添加请求头文件
				.url(url).post(body).build();
		Response response = client.newCall(request).execute();
		return response.body().string();
	}
}
